package org.gruposp2p.aula.gwt.client.content;

import java.util.List;
import org.gruposp2p.aula.gwt.client.AulaApplication;
import org.gruposp2p.aula.gwt.client.AulaConstants;
import org.gruposp2p.aula.gwt.client.event.AulaEventBus;
import org.gruposp2p.aula.gwt.client.event.CourseChangeEvent;
import org.gruposp2p.aula.gwt.client.event.CourseChangeHandler;
import org.gruposp2p.aula.gwt.client.event.CoursesChangeEvent;
import org.gruposp2p.aula.gwt.client.event.CoursesChangeHandler;
import org.gruposp2p.aula.gwt.client.model.Course;
import org.gruposp2p.aula.gwt.client.model.Link;
import org.gruposp2p.aula.gwt.client.representation.CoursegroupRepresentation;
import org.gruposp2p.aula.gwt.client.representation.CoursesRepresentation;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class AulaTitlePanelManager implements CoursesChangeHandler, CourseChangeHandler {
	
	private String STYLE_NAME = "aulaTitlePanelManager";
	private AulaApplication aulaApplication;
	private AulaConstants constants;
	private List<Course> courses;
	private Course selectedCourse;
	private ListBox coursesBox;
	private List<Link> coursegroupsLinks;
	private ListBox coursegroupsBox;
	
	/**
	 * The panel that contains the title widget and links.
	 */
	public static interface CwConstants extends Constants {
		  String mainTitle ();
		  String mainSubTitle ();	
		  String[] AulaTitlePanelManagerCourses();	  
		  String AulaTitlePanelManagerCourseCombo();
		  String AulaTitlePanelManagerCoursegroupCombo();
		  String CourseWithoutCoursegroupMsg();
	}
	
	public AulaTitlePanelManager (AulaApplication aulaApplication) {
		this.aulaApplication = aulaApplication;
		this.constants = aulaApplication.getConstants();
		AulaEventBus.getInstance().registerToCoursesChange(this);
		AulaEventBus.getInstance().registerToCourseChange(this);
		CoursesRepresentation.getInstance().getCourses();
	}
	
	public final Widget createWidget() {
	    String pageTitle = "<h1>" + constants.mainTitle() + "</h1><h2>"
	        + constants.mainSubTitle() + "</h2>";
	    FlexTable titlePanel = new FlexTable();
	    titlePanel.setStyleName(STYLE_NAME);
	    titlePanel.setWidget(0, 0, aulaApplication.getImages().aula01().createImage());
	    titlePanel.setWidget(0, 1, new HTML(pageTitle));
	    HorizontalPanel emptyPanel = new HorizontalPanel();
	    emptyPanel.setWidth("100");
	    titlePanel.setWidget(0, 2, emptyPanel);	    
	    coursesBox = new ListBox(false);
	    String[] courses = constants.AulaTitlePanelManagerCourses();
	    for (int i = 0; i < courses.length; i++) {
	    	coursesBox.addItem(courses[i]);
	    }
	    coursesBox.addChangeHandler(new CoursesChangeHandler());
	    coursesBox.ensureDebugId("coursesBox");
	    coursesBox.setStyleName("coursesBox");
	    VerticalPanel coursesBoxPanel = new VerticalPanel();
	    coursesBoxPanel.setSpacing(4);
	    coursesBoxPanel.add(new HTML(constants.AulaTitlePanelManagerCourseCombo()));
	    coursesBoxPanel.add(coursesBox);
	    titlePanel.setWidget(0, 3, coursesBoxPanel);	    
	    HorizontalPanel emptyPanel1 = new HorizontalPanel();
	    emptyPanel.setWidth("100");
	    titlePanel.setWidget(0, 4, emptyPanel1);	    
	    coursegroupsBox = new ListBox(false);
	    coursegroupsBox.addChangeHandler(new CoursegroupsChangeHandler());
	    coursegroupsBox.ensureDebugId("coursegroupsBox");
	    coursegroupsBox.setStyleName("coursegroupsBox");	    
	    VerticalPanel coursegroupsBoxPanel = new VerticalPanel();
	    coursegroupsBoxPanel.setSpacing(4);
	    coursegroupsBoxPanel.add(new HTML(
	    		constants.AulaTitlePanelManagerCoursegroupCombo()));
	    coursegroupsBoxPanel.add(coursegroupsBox);
	    titlePanel.setWidget(0, 5, coursegroupsBoxPanel);	    
	    return titlePanel;
	}
	
	private class CoursesChangeHandler implements ChangeHandler{
		@Override
		public void onChange(ChangeEvent event) {	
			int selectedIndex = coursesBox.getSelectedIndex();
			Course course = courses.get(selectedIndex);
			CourseChangeEvent courseChangeEvent = new CourseChangeEvent();
			courseChangeEvent.setCourse(course);
			AulaEventBus.getInstance().fireEvent(courseChangeEvent);
		}
	}
	
	private class CoursegroupsChangeHandler implements ChangeHandler{
		@Override
		public void onChange(ChangeEvent event) {	
			int selectedIndex = coursegroupsBox.getSelectedIndex();
			GWT.log("selectedIndex: " + selectedIndex, null);
			CoursegroupRepresentation.getInstance().getCoursegroup(
					coursegroupsLinks.get(selectedIndex));

		}
	}
	
	public void updateCoursegroupsBox (List<Link> coursegroupsLinks) {
		this.coursegroupsLinks = coursegroupsLinks;
		coursegroupsBox.clear();
		if (coursegroupsLinks.size() > 0) {
			coursegroupsBox.setEnabled(true);
		    for (Link coursegroupLink : coursegroupsLinks) {
		    	coursegroupsBox.addItem(coursegroupLink.getName());		    	
		    }
			CoursegroupRepresentation.getInstance().getCoursegroup(
					coursegroupsLinks.get(0));
		} else {
			coursegroupsBox.addItem(constants.CourseWithoutCoursegroupMsg());
			coursegroupsBox.setEnabled(false);
		}	
	}	
	
	@Override
	public void processCourses(CoursesChangeEvent event) { 
		courses = event.getCourses();
	    selectedCourse = courses.get(0);
	    CourseChangeEvent courseChangeEvent = new CourseChangeEvent();
	    courseChangeEvent.setCourse(selectedCourse);
	    AulaEventBus.getInstance().fireEvent(courseChangeEvent);
		coursesBox.clear();
	    for (Course course : courses) {
	    	coursesBox.addItem(course.getLink().getName());
	    }
	    updateCoursegroupsBox(selectedCourse.getCoursegroupLinks());
	}

	@Override
	public void processCourse(CourseChangeEvent event) {
		selectedCourse = event.getCourse();
		updateCoursegroupsBox(selectedCourse.getCoursegroupLinks());
	}
	
}
